<?php
		$CurrentTime = date('Y-m-d H:i:s');
		$DAY = date('d');
		$SchedDay = 'mon'.date('w');

if (isset($_POST['ACT'])) {
	$Type = 1;
	if ($_POST['ACT'] == 'HDREQ') {
	$_POST['TimeStart'] = $_POST['Day'].' '.$_POST['HourStart'];
	$_POST['TimeEnd'] = $_POST['Day'].' '.$_POST['HourEnd'];
	$Type = 2;
	}
				$QAction = "INSERT timeclock_vacs
				(RefUser, TimeStart, TimeEnd, Status,Observations,Type,RefOffice,Time,sysUser) 
				VALUES ({$_SESSION['UtID']},'{$_POST['TimeStart']}','{$_POST['TimeEnd']}',1,'{$_POST['Observations']}',{$Type},{$_SESSION[$SESSOF]},'{$CurrentTime}',{$_SESSION['UtID']})";
				$Action = $GLOBALS['CONN']->db_query($QAction); 
				
				if (isset($Action)) {
					$Msg_CONFIRM = "Pedido Submetido";
				}
}
if(isset($_GET['action'])) {
	save_times($_GET['action']);
}

if(isset($_POST['SubmitDelay'])) {

				$Justifications = $GLOBALS['CONN']->db_query("UPDATE timeclock_delays 
				SET Justify = '{$_POST['justification']}', Status = 2,
				Description = '{$_POST['Description']}'
				WHERE ref = {$_POST['ref']}"); 
}
if(isset($_POST['SubmitOff'])) {

	$COff = "SELECT * FROM timeclock_delays WHERE main_user_ref = '{$_SESSION['UtID']}'
					AND DATE(EventTime) = DATE('$CurrentTime')
					AND Status = 1
					ORDER BY ref DESC LIMIT 0,1";
	$Off = $GLOBALS['CONN']->db_array($COff); 
			$TimeOff = date("H:i:s", (strtotime($CurrentTime)-strtotime($Off[1]['EventTime'])));
				$QJustifications = "UPDATE timeclock_delays 
				SET Justify = '{$_POST['justification']}',
				Description = '{$_POST['Description']}' ,
				Status = 2,
				delaytime = '".strtotime($TimeOff)."'
				WHERE ref = {$Off[1]['ref']}";
				$Justifications = $GLOBALS['CONN']->db_query($QJustifications); 
				
			$OpInfo = $_POST['OpOff'].$CurrentTime.';';
				$UOp = "UPDATE timeclock_operations
				SET Breaks = '{$OpInfo}'
				WHERE ref = {$_POST['refOp']}";
				$GLOBALS['CONN']->db_query($UOp); 
}
	$QCallTime = "SELECT * FROM timeclock_operations WHERE main_users_ref = {$_SESSION['UtID']}
					AND DATE(timeclock_operations.time_main) = DATE('$CurrentTime')
					ORDER BY ref DESC LIMIT 0,1";
	$CallTime = $GLOBALS['CONN']->db_array($QCallTime); 
	
	if (isset($CallTime[1]) && $CallTime[1]['InTime'] == '') {
		
		$QCreateInTime = "UPDATE timeclock_operations 
		SET InTime = '{$CurrentTime}' WHERE ref = {$CallTime[1]['ref']}";
		$GLOBALS['CONN']->db_query($QCreateInTime); 
	}
	
	if (isset($CallTime[1]) && $CallTime[1]['InTime'] != '') {
	$QTimeSetts = "SELECT 
						TIME(timeclock_profiles.tolerance) AS tolerance,
						TIME(timeclock_ttables.entrytime) AS entrytime,
						TIME(timeclock_ttables.leavetime) AS leavetime,
						TIME(timeclock_ttables.lunchstart) AS lunchstart,
						TIME(timeclock_ttables.lunchend) AS lunchend
				 FROM timeclock_profiles
					INNER JOIN timeclock_ttables ON timeclock_ttables.ref = timeclock_profiles.{$SchedDay}
					WHERE main_users_ref = {$_SESSION['UtID']}";
	$TimeSetts = $GLOBALS['CONN']->db_array($QTimeSetts); 
	
	$QAdminTimeSetts = "SELECT * FROM administration_timeclock ORDER BY SettingsTime DESC LIMIT 0,1";
	$AdminTimeSetts = $GLOBALS['CONN']->db_array($QAdminTimeSetts); 

			$Tolerance = split(":",$TimeSetts[1]['tolerance']);
			$Delay = $Tolerance[0].":".$Tolerance[1].":".$Tolerance[2];
			$Tolerance = $Tolerance[1]*60;
	} 
	
if (isset($_FILES['DocFILE']))  {
	move_uploaded_file($_FILES['DocFILE']['tmp_name'],$GLOBALS['DOC'].'TimeClock/'.$_FILES['DocFILE']['name']);
				$UDelays = "UPDATE timeclock_delays 
				SET File = '{$_FILES['DocFILE']['name']}'
				WHERE ref = {$_POST['EventRef']}";
				$Delays = $GLOBALS['CONN']->db_query($UDelays); 
}
	
if (isset($_POST['DelOk']))  {
			$UDelays = "UPDATE timeclock_delays 
			SET Status = 8
			WHERE ref = {$_POST['DelOk']}";
			$Delays = $GLOBALS['CONN']->db_query($UDelays); 
}
if (isset($_POST['VacOk']))  {
			$UDelays = "UPDATE timeclock_delays 
			SET Status = 8
			WHERE ref = {$_POST['VacOk']}";
			$Delays = $GLOBALS['CONN']->db_query($UDelays); 
}
?>